package xyz.zhzby.cyzx.ucenter.util;

import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;
import io.swagger.annotations.ApiModel;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @author zby
 * @date 2021/12/17
 */
@Component
@ApiModel(description = "邮件相关封装")
public class MailUtils implements InitializingBean {

    @Autowired
    private JavaMailSenderImpl mailSender;

    @Value("${spring.mail.username}")
    private String from;

    private static JavaMailSenderImpl MAIL_SENDER;

    private static String FROM;



    @Override
    public void afterPropertiesSet() throws Exception {
        MAIL_SENDER = mailSender;
        FROM = from;
    }

    /**
     * @Param [subject, context, to] [主题， 内容， 接受者]
     * @Return void []
     * @Author zby
     * @Description 发送简单文本邮件
     */
    public static void sendTextMail(String subject, String context, String to) {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject(subject);
        mailMessage.setText(context);
        mailMessage.setFrom(FROM);
        mailMessage.setTo(to);
        MAIL_SENDER.send(mailMessage);
    }

    /**
     * @Param [to] [邮件接受者]
     * @Return void []
     * @Author zby
     * @Description 发送验证码邮件（6位数字验证码）
     */
    public static void sendVerificationCode(String to, String code) throws Exception {
        MimeMessage message = MAIL_SENDER.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setSubject("验证码");
        String html = "<p><strong>亲爱的用户：</strong></p>\n" +
                "<p>您正在验证帐号，验证码是：</p>\n" +
                "<p style=\"font-size: xx-large; color: #40caba\">"
                + code +
                "</p>\n" +
                "<p>5分钟内有效，为了您的帐号安全，请勿泄露给他人。如非本人操作无需处理。</p>";
        helper.setText(html, true);
        helper.setFrom(FROM);
        helper.setTo(to);
        MAIL_SENDER.send(message);
    }

    /**
     * @Param [subject, context, to, files] [主题， 内容， 接受者, 附件（数量不定）]
     * @Return void []
     * @Author zby
     * @Description 复杂邮件发送（可附带附件）
     */
    public static void sendComplexMail(String subject, String context, String to, File... files) throws Exception {
        MimeMessage mimeMessage = MAIL_SENDER.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setSubject(subject);
        helper.setText(context);
        helper.setFrom(FROM);
        helper.setTo(to);
        for (File file : files) {
            String fileName = MimeUtility.encodeText(file.getName());
            helper.addAttachment(fileName, file);
        }
        MAIL_SENDER.send(mimeMessage);
    }

}
